<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="报警标题" prop="alarmTitle">
        <el-input
            v-model="queryParams.alarmTitle"
            placeholder="请输入报警模板标题"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板类型" prop="alarmProfileKey">
        <el-input
            v-model="queryParams.alarmProfileKey"
            placeholder="请输入报警模板key"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="alarmProfileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="报警标题" align="center" prop="alarmTitle"/>
      <el-table-column label="模板类型" align="center" prop="alarmProfileKey"/>
      <el-table-column label="事件级别" align="center" prop="alarmLevel">
        <template #default="scope">
          <el-tag v-if="scope.row.alarmLevel=== 0" type="success">低</el-tag>
          <el-tag v-else-if="scope.row.alarmLevel === 1" type="danger">中</el-tag>
          <el-tag v-else-if="scope.row.alarmLevel === 2" type="danger">高</el-tag>
          <el-tag v-else type="info">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="报警内容" align="center" prop="alarmContent" width="500px" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px">
        <template #default="scope">
          <el-button
              link type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['coal:alarmProfile:edit']"
          >修改
          </el-button>
          <el-button
              link type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['coal:alarmProfile:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改报警信息模板对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="alarmProfileRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报警标题" prop="alarmTitle">
          <el-input v-model="form.alarmTitle" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="模板类型" prop="alarmProfileKey">
          <el-input v-model="form.alarmProfileKey" placeholder="请输入报警模板类型"/>
        </el-form-item>
        <el-form-item label="报警内容" prop="alarmContent" >
          <editor v-model="form.alarmContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="事件级别" prop="alarmLevel">
          <el-radio-group v-model="form.alarmLevel">
            <el-radio-button :label=0>低</el-radio-button>
            <el-radio-button :label=1 >中</el-radio-button>
            <el-radio-button :label=2 >高</el-radio-button>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AlarmProfile">
import {
  listAlarmProfile,
  getAlarmProfile,
  delAlarmProfile,
  addAlarmProfile,
  updateAlarmProfile
} from "@/api/manager/alarmProfile";

const { proxy } = getCurrentInstance();

const alarmProfileList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    alarmTitle: null,
    alarmProfileKey: null,
    alarmContent: null,
    alarmLevel: null,
    enableFlag: null,
    deleted: null,
  },
  rules: {
    alarmTitle: [
      {required: true, message: "报警标题不能为空", trigger: "blur"}
    ],
    alarmProfileKey: [
      {required: true, message: "报警模板key不能为空", trigger: "blur"}
    ],
    alarmContent: [
      {required: true, message: "报警内容不能为空", trigger: "blur"}
    ],
    alarmLevel: [
      {required: true, message: "事件级别(0低 1中 2高)不能为空", trigger: "blur"}
    ],
    enableFlag: [
      {required: true, message: "使能标识不能为空", trigger: "blur"}
    ],
    deleted: [
      {required: true, message: "逻辑删除标识,0:未删除,1:已删除不能为空", trigger: "blur"}
    ],
    createBy: [
      {required: true, message: "创建者不能为空", trigger: "blur"}
    ],
    createTime: [
      {required: true, message: "创建时间不能为空", trigger: "blur"}
    ],
    updateBy: [
      {required: true, message: "更新者不能为空", trigger: "blur"}
    ],
    updateTime: [
      {required: true, message: "更新时间不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询报警信息模板列表 */
function getList() {
  loading.value = true;
  listAlarmProfile(queryParams.value).then(response => {
    alarmProfileList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    alarmTitle: null,
    alarmProfileKey: null,
    alarmContent: undefined,
    alarmLevel: null,
    enableFlag: null,
    deleted: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("alarmProfileRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  console.log("1-------------------")
  console.log(form.value.alarmContent)
  reset();
  console.log("2-------------------")
  console.log(open.value)
  open.value = true;
  console.log("3-------------------")
  // title.value = "添加报警信息模板";
  // console.log("4-------------------")
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getAlarmProfile(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改报警信息模板";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["alarmProfileRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateAlarmProfile(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAlarmProfile(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除报警信息模板编号为"' + _ids + '"的数据项？').then(function () {
    return delAlarmProfile(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manager/alarmProfile/export', {
    ...queryParams.value
  }, `alarmProfile_${new Date().getTime()}.xlsx`)
}

getList();
</script>
